package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 负责与dubbo的服务进行交互
 */
@Service
public class RecommendUserService {
    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    /**
     * 查询形参中的userId对应的用户的今日佳人
     *
     * @param userId 用户id
     * @return 今日佳人对象
     */
    public TodayBest queryTodayBest(Long userId) {
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        if (null == recommendUser) {
            return null;
        }

        TodayBest todayBest = new TodayBest();
        todayBest.setId(recommendUser.getUserId());

        // 设定缘分值(向下取整,因为有超过100的值)
        double score = Math.floor(recommendUser.getScore());
        todayBest.setFateValue(Double.valueOf(score).longValue());

        return todayBest;
    }

    public PageInfo<RecommendUser> queryRecommendUserList(Long id, Integer page, Integer pageSize) {
        return recommendUserApi.queryPageInfo(id, page, pageSize);
    }

    /**
     * 查询探花的卡片列表
     *
     * @param id
     * @param count
     * @return
     */
    public List<RecommendUser> queryCardList(Long id, int count) {
        return recommendUserApi.queryCardList(id,count);
    }

    /**
     * 查询b对于a的缘分值
     *
     * @param userId   b的id
     * @param toUserId a的id
     * @return b对于a缘分值
     */
    public Double queryScore(Long userId, Long toUserId) {
        Double score = recommendUserApi.queryScore(userId, toUserId);
        if (ObjectUtil.isNotEmpty(score)) {
            return score;
        }
        // 设定一个默认缘分值
        return 98d;
    }

}